


import json
import logging
import selenium.webdriver
import appium.webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from config import BASE_PATH


class DriverUtils:
    # 网站驱动对象存储的私有属性
    __web_driver = None

    # 获取网站的驱动对象
    @classmethod
    def get_web_driver(cls):
        if cls.__web_driver is None:
            cls.__web_driver = selenium.webdriver.Chrome()
            cls.__web_driver.maximize_window()
            cls.__web_driver.implicitly_wait(30)
        return cls.__web_driver

    # 关闭浏览器驱动对象的开关
    __web_key= True
    # 修改关闭浏览器驱动对象开关值
    @classmethod
    def change_web_key(cls,key):
        cls.__web_key = key
    # 关闭网站的驱动对象
    @classmethod
    def quit_web_driver(cls):
        if cls.__web_driver is not None and cls.__web_key:
            cls.__web_driver.quit()
            cls.__web_driver = None

    # app驱动对象存储的私有属性
    __app_driver = None

    # 获取app的驱动对象
    @classmethod
    def get_app_driver(cls):
        if cls.__app_driver is None:
            desired_caps = {
                "platformName": "Android",
                "platformVersion": "12",
                "deviceName": "zxw",
                "appPackage": "com.ss.android.article.news",
                "appActivity": "com.ss.android.article.news.activity.MainActivity",
                "noReset": True
            }

            cls.__app_driver = appium.webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
        return cls.__app_driver
        #     desired_caps = {
        #         "platformName": "Android",  # 平台名称
        #         "platformVersion": "12",  # 平台版本
        #         "deviceName": "无法arrogance",  # 设备名称
        #         "appPackage": "com.smile.gifmaker",  # app包名
        #         "appActivity": "com.yxcorp.gifshow.HomeActivity",  # app启动页
        #         "unicodeKeyboard": True,  # 使用自带输入法，输入中文时填 true
        #         "resetKeyboard": True  # 执行完程序恢复原来的输入法
        #     }
        #     cls.__app_driver = appium.webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)
        # return cls.__app_driver

    # 关闭浏览器驱动对象的开关
    __app_key = True

    # 修改关闭浏览器驱动对象开关值
    @classmethod
    def change_app_key(cls, appkey):
        cls.__app_key = appkey
    # 关闭app的驱动对象
    @classmethod
    def quit_app_driver(cls):
        if cls.__app_driver is not None and cls.__app_key:
            cls.__app_driver.quit()
            cls.__app_driver = None

# 函数:公用的获取任意元素文本
def get_element_text(driver, xpath_str):
    # 获取元素文本
    # msg = DriverUtils.get_driver().find_element(By.XPATH, xpath_str).text
    try:
        msg = WebDriverWait(driver, 10, 1). \
            until(lambda x: x.find_element(By.XPATH, xpath_str)).text
        print(msg)
    except Exception as e:
        logging.error(f"没有获取到{xpath_str}的元素对象文本!")
        msg = None
    # 返回获取的文本
    return msg
# # 函数：公用的获取任意元素文本
# def get_element_text(driver, xpath_str):
#     """
#     :param driver: 驱动对象
#     :param xpath_str: 元素对象xpath
#     :return: 元素对象文本
#     """
#     try:
#         msg = (WebDriverWait(driver, 10, 1).
#                until(lambda x: x.find_element(By.XPATH, xpath_str)).text())
#         print(msg)
#         logging.info(f"找到{xpath_str}的元素对象文本")
#     except Exception as e:
#         logging.error(f"没有找到{xpath_str}的元素对象文本")
#         msg = None
#     return msg


# 函数:根据文本判断当前页面是否有对应的元素对象
def element_is_exist_by_text(driver, is_app, text):
    """
    :param driver: 驱动对象
    :param is_app: 是否是app
    :param text: 元素对象文本
    :return:
    """
    if is_app:
        xpath = f"//*[@text='{text}']"
    else:
        xpath = f"//*[text()='{text}']"

    try:
        is_suc = (WebDriverWait(driver, 10, 1).
                  until(lambda x: x.find_element(By.XPATH, xpath)))
        logging.info(f"找到{text}的元素对象")
        print(text)
    except Exception as e:
        is_suc = None
        logging.error(f"没有找到{text}的元素对象")
    return is_suc


# 数据驱动读取数据的函数
def read_data(file_name):
    """
    :param file_name: 文件名称
    :return:
    """
    # 完整测试数据文件名
    file_path = BASE_PATH + f"/Data/{file_name}.json"
    # 定义空列表存储数据
    data_list = []
    # 1.读取json文件所有数据
    with open(file_path, encoding="utf-8") as f:
        data = json.load(f)
    # 2.遍历第一层的键值
    for i in data.values():
        # 3.获取该遍历的值的所有键值,并且强转换成列表# 4.需要保存读取到的测试数据
        data_list.append(list(i.values()))
    # 5.返回测试数据
    return data_list
